package com.guldan.thrall.web.controller;


import com.guldan.core.constants.Constant;
import com.guldan.core.controller.BaseController;
import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.enums.NewsTypeEn;
import com.guldan.core.response.ResponseJson;
import com.guldan.core.search.Pagination;
import com.guldan.jaina.dto.news.NewsDTO;
import com.guldan.jaina.dto.news.NewsSearchDTO;
import com.guldan.jaina.provider.INewsProvider;
import com.guldan.thrall.util.Uploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by Silas.
 * Date: 2017/1/24
 * Time: 12:04
 */
@Controller
@RequestMapping("/news")
public class NewsController extends BaseController {

    @Autowired
    private INewsProvider iNewsProvider;

    //获取单个居民信息
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    NewsDTO get(@RequestBody NewsDTO dto) {
        ResponseDTO<NewsDTO> response = iNewsProvider.get(dto.getNewsId());
        return response.getData();
    }

    //新增/保存单个居民信息
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    NewsDTO save(@RequestBody NewsDTO dto) throws Exception {
        ResponseDTO<NewsDTO> response = iNewsProvider.save(dto);
        if (response.isSuccess())
            return response.getData();
        else {
            throw response.getException();
        }
    }

    //删除单个居民信息
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    Boolean delete(@RequestBody NewsDTO dto) {
        ResponseDTO<Boolean> response = iNewsProvider.delete(dto.getNewsId());
        return response.getData();
    }

    //获取新闻公告信息列表
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    Pagination<NewsDTO> list(@RequestBody NewsSearchDTO dto) {
        ResponseDTO<Pagination<NewsDTO>> response = iNewsProvider.list(dto);
        return response.getData();
    }

    //获取系统通知信息列表
    @RequestMapping(value = "/listNotice", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    List<NewsDTO> listNotice(@RequestBody NewsSearchDTO dto) {

        dto.setIsNotice(true);
        dto.setNewsType(NewsTypeEn.notice.getCode());

        ResponseDTO<Pagination<NewsDTO>> response = iNewsProvider.list(dto);
        return response.getData().getList();
    }

    //图片上传
    @RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    String uploadFile(HttpServletRequest request,
                      HttpServletResponse response) throws Exception {


        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());

        if (multipartResolver.isMultipart(request)) {
            logger.info("sssss");
        }

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        Uploader up = new Uploader(request);
        String filePath = "resources/attachment/citizen/updatearea/up";

        up.setSavePath(filePath);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload();

        String callback = request.getParameter("callback");

        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize() + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";

        result = result.replaceAll("\\\\", "\\\\");
        logger.info("result : " + result);
        if (callback == null) {
            response.setStatus(200);
            response.getWriter().print(result);

        } else {
            response.getWriter().print("<script>" + callback + "(" + result + ")</script>");
        }

        return result;

    }

}
